package demo.um.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import demo.um.model.UserInfo;

@Stateless
public class UserService {

	@PersistenceContext
	private EntityManager em;

	@SuppressWarnings("unchecked")
	@TransactionAttribute(TransactionAttributeType.NEVER)
	public List<UserInfo> getUsers() {
		Query query = em.createQuery("select u from UserInfo u");
		return (List<UserInfo>) query.getResultList();
	}

	@TransactionAttribute(TransactionAttributeType.NEVER)
	public UserInfo getUserByName(String name) {
		Query query = em.createQuery("select u from UserInfo u where u.name = ?");
		query.setParameter(1, name);
		return (UserInfo) query.getSingleResult();
	}

	public void saveUser(UserInfo user) {
		em.merge(user);
	}

	public void deleteUser(UserInfo user) {
		em.remove(em.merge(user));
	}

}